const express = require("express")
const path = require("path")
const app = express()
const fs = require("fs")
app.use(express.static("../public"))

const cors = require("cors")

app.use(cors())

const sharp = require("sharp")

app.get("/image/:img", (req, res) => {
    console.log(req.params);
    console.log(req.query);
    const img_name = '../public/images/'+ req.params.img
    const resize_img_name = '../image/' + req.params.img
    if (!fs.existsSync(img_name)) {
         return res.send({
             code:300,
             msg:"未发现相关图片资源"
         })
    }
    if (fs.existsSync(resize_img_name)) {
        fs.unlink("../image/" + req.params.img, (err) => {
            if (err) {

                throw err;

            }
        })
    }

    // @ts-ignore
    const reszieArr = req.query.param.split("y")
    console.log(reszieArr);
    sharp(img_name).resize(reszieArr[0] * 1, reszieArr[1] * 1).toFile(resize_img_name, (err) => {
        if (err) return res.send({ status: 300, msg: "系统发生错误" })
        res.sendFile(path.resolve(__dirname, resize_img_name))
    })
})



app.listen(8090)